package com.zp.business.controller.patVisit;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.zp.business.entity.patVisit.bo.PatVisitBo;
import com.zp.business.entity.patVisit.vo.PatVisitVo;
import com.zp.business.service.patVisit.IPatVisitService;
import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.validate.AddGroup;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.excel.utils.ExcelUtil;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 患者住院信息控制器
 * 前端访问路由地址为:/business/patVisit
 *
 * @author zhang peng
 * @ date 2024-06-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/patVisit")
public class PatVisitController extends BaseController {

    private final IPatVisitService iPatVisitService;

    /**
     * 查询患者住院信息列表 (分页)
     */
    @SaCheckPermission("finance:patVisit:list")
    @GetMapping("/selectPage")
    public TableDataInfo<PatVisitVo> selectPage(PatVisitBo bo, PageQuery pageQuery) {
        return iPatVisitService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询患者住院信息列表 (不分页)
     */
    @SaCheckPermission("finance:patVisit:list")
    @GetMapping("/list")
    public R<List<PatVisitVo>> list(PatVisitBo bo) {
        List<PatVisitVo> list = iPatVisitService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出患者住院信息列表
     */
    @SaCheckPermission("business:patVisit:export")
    @Log(title = "患者住院信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PatVisitBo bo, HttpServletResponse response) {
        List<PatVisitVo> list = iPatVisitService.queryList(bo);
        ExcelUtil.exportExcel(list, "患者住院信息", PatVisitVo.class, response);
    }

    /**
     * 获取患者住院信息详细信息
     *
     * @param visitId 主键
     */
    @SaCheckPermission("business:patVisit:list")
    @GetMapping("/{visitId}")
    public R<PatVisitVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable String visitId) {
        return R.ok(iPatVisitService.queryById(visitId));
    }

    /**
     * 新增患者住院信息
     */
    @SaCheckPermission("business:patVisit:add")
    @Log(title = "患者住院信息", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PatVisitBo bo) {
        return toAjax(iPatVisitService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改患者住院信息
     */
    @SaCheckPermission("business:patVisit:edit")
    @Log(title = "患者住院信息", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PatVisitBo bo) {
        return toAjax(iPatVisitService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除患者住院信息
     *
     * @param visitIds 主键串
     */
    @SaCheckPermission("business:patVisit:remove")
    @Log(title = "患者住院信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{visitIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable String[] visitIds) {
        return toAjax(iPatVisitService.deleteWithValidByIds(Arrays.asList(visitIds), true) ? 1 : 0);
    }
}
